package com.vunguyen.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.vunguyen.model.Canbo;

@Repository
public class UserDAOImpl implements UserDAO {
	
	@Autowired
	private SessionFactory sessionFactory;

	@SuppressWarnings("unchecked")
	@Override
	public boolean checkLogin(int username, String password) {
		List<Canbo> taikhoans = new ArrayList<Canbo>();
		taikhoans =  sessionFactory.getCurrentSession()
									.createQuery(
									"FROM Canbo WHERE idCanBo = '"+username+"' and password = '"+password+"'")
									.list();
		if(taikhoans.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public Canbo getTaikhoanbyUsername(int username) {
		try {
			return (Canbo) sessionFactory.getCurrentSession()
					.createQuery("FROM Canbo WHERE idCanBo='" + username + "'")
					.list().get(0);
		} catch (Exception e) {
			return null;
		}finally{
		}
	}

	@Override
	public Canbo getTenCanBoById(int idcanbo) {
		return (Canbo) sessionFactory.getCurrentSession().createQuery("from Canbo where idCanBo='"+idcanbo+"'").list().get(0);
	}



}
